package com.jianyun.jyzs.model;

import android.util.Log;

import com.google.gson.Gson;
import com.jianyun.jyzs.bean.NewSign2Bean;
import com.jianyun.jyzs.bean.NewSignRecodeBean;
import com.jianyun.jyzs.http.Api;
import com.jianyun.jyzs.http.CustomCallback;
import com.jianyun.jyzs.http.RetrofitHelper;
import com.jianyun.jyzs.model.imdoel.INewSignRecordModel;
import com.jianyun.jyzs.utils.LoginCache;

import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yangshijun on 2018/6/13.
 */

public class NewSignRecordModel implements INewSignRecordModel {

    private static NewSignRecordModel model;

    private NewSignRecordModel() {
    }

    public static NewSignRecordModel getInstance() {
        if (model == null)
            model = new NewSignRecordModel();
        return model;
    }

    @Override
    public void getRecordeByDate(String enterpriseCode, String userId, String date, final OnGetRecordListener onGetRecordListener) {
        Api api = RetrofitHelper.builderNewSignHttp().create(Api.class);
        String serviceUrl = LoginCache.getInstance().getSignVersion().getServiceUrl();
        Log.i("test","记录："+userId);
        api.getSignDataList2(serviceUrl,"GetSignDayList", enterpriseCode, userId, date)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Object>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        onGetRecordListener.onFailed("获取数据失败");
                    }

                    @Override
                    public void onNext(Object o) {
                        Gson gson = new Gson();
                        String s = gson.toJson(o);
                        NewSign2Bean sign2Bean = gson.fromJson(s, NewSign2Bean.class);
                        if (sign2Bean.getResult()) {
                            onGetRecordListener.onGetSuccess(sign2Bean.getSignDetail());
                        } else {
                            onGetRecordListener.onFailed(sign2Bean.getMessage());
                        }
                    }
                });
                /*.subscribe(new CustomCallback<NewSignRecodeBean>() {
                    @Override
                    protected void onResponse(NewSignRecodeBean response) {
                        if (response.getResult()) {
                            List<NewSignRecodeBean.SignDetail> signDetail = response.getSignDetail();
                            onGetRecordListener.onGetSuccess(signDetail);
                        } else {
                            onGetRecordListener.onFailed(response.getMessage());
                        }
                    }

                    @Override
                    protected void onException(String msg) {
                        onGetRecordListener.onFailed("获取数据失败");
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        onGetRecordListener.onFailed("获取数据失败");
                    }
                });*/
    }
}
